package com.augurit.wf.app.shouwen.web;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.augurit.common.base.action.CrudActionSupport;
import com.augurit.common.syscode.service.ISysCodeService;
import com.augurit.common.syscode.web.form.SysCodeForm;
import com.augurit.common.system.util.WebContextUtils;
import com.augurit.common.system.web.auth.form.LoginUserForm;
import com.augurit.wf.app.shouwen.service.IOaShouwenService;
import com.augurit.wf.app.shouwen.web.form.OaShouwenForm;
import com.augurit.wf.bi.web.form.WfBusInstanceTaskForm;
import com.augurit.wf.common.constant.WfConstant;
import com.augurit.wf.common.entity.OaFawen;
import com.augurit.wf.common.service.IWfBusClientService;
import com.augurit.wf.common.util.WfUtils;
import com.augurit.wf.common.web.WfBusClientAction;

@SuppressWarnings("serial")
@Results({
	@Result(name = CrudActionSupport.RELOAD, location = "oa-shouwen.action", type = "redirect"),
	@Result(name = CrudActionSupport.SUCCESS, location = "/wf/app/shouwen/oa-shouwen.jsp"),
	@Result(name = CrudActionSupport.INPUT, location = "/wf/app/shouwen/oa-shouwen-input.jsp"),
	@Result(name = "cy", location = "/wf/app/shouwen/oa-chuanyue-input.jsp"),
	@Result(name = WfConstant.PRINT, location = WfConstant.PRINT_JSP)
})
public class OaShouwenAction extends WfBusClientAction<OaShouwenForm>{

	@Autowired
	private IOaShouwenService oaShouwenService;

	@Autowired
    private ISysCodeService syscodeService;

	
	private OaShouwenForm form;
	private Long id;
	private Long[] checkedIds;
	private Page<OaShouwenForm> page = new Page<OaShouwenForm>(10);

	
	/**
     * 保存新增或者修改的公告
     */
    public String wfBusSave() throws Exception {
    	WfBusInstanceTaskForm task = this.wfBusSave((IWfBusClientService)oaShouwenService, form, OaFawen.class.getSimpleName());
    	WfUtils.renderWfBusSaveJson(task, form);
        return null;
    }
    
	/**
	 * 分页查询
	 */
	@Override
	public String list() throws Exception {
		oaShouwenService.search(page, form);
		return SUCCESS;
	}

	/**
	 * 新增或修改
	 */
	@Override
	public String input() throws Exception {
		this.initSysCodeItem();
		this.initWfOpinions();
		return INPUT;
	}
	
	 /**
     * 传阅文件的新增或修改
     */
    public String cyinput() throws Exception {
        this.initSysCodeItem();
        
//        @SuppressWarnings({"unchecked"})
//        Map<String, List<TraceRecord>> map = WfUtils.loadWfOpinionsByTaskId(getTaskInstanceId(), wfService);
//        BaseAction.getRequest().setAttribute(WfOpinionTag.WF_OPINIONS, map);
        return "cy";
    }
    
	private void initSysCodeItem() {
	    // 紧急程度
	    List<SysCodeForm> urgencyList = syscodeService.getItems("OA_urgency");
	    // 秘密等级
	    List<SysCodeForm> secretList = syscodeService.getItems("OA_secret");
	    //公文种类
	    List<SysCodeForm> docTypeList = syscodeService.getItems("OA_doc_type");
	        
	    super.getRequest().setAttribute("urgencyList", urgencyList);
	    super.getRequest().setAttribute("secretList", secretList);
	    super.getRequest().setAttribute("docTypeList", docTypeList);
	}
	 
	public String getTaskInstanceId(){
//	   return getParameter(IWfBusClient.PARAM_TASK_INST_DBID);
		return null;
	}
	
	/**
	 * 打印承办单
	 */
	public String printCbd() throws Exception {
		String taskInstanceIdStr = getParameter("taskInstanceId");
		if(taskInstanceIdStr != null && taskInstanceIdStr.trim().length() > 0){
//			Map<String, List<TraceRecord>> opinions = WfUtils.loadWfOpinionsByTaskId(taskInstanceIdStr, wfService);
			
//			List<TraceRecord> pishi = opinions.get("leader_approves");
//			List<TraceRecord> niban = opinions.get("director_propose");
//			List<TraceRecord> fenban = opinions.get("transmit_review");
			
//			form.setPishiOpinion(WfUtils.printOpitions(pishi));
//			form.setNibanOpinion(WfUtils.printOpitions(niban));
//			form.setFenbanOpinion(WfUtils.printOpitions(fenban));
		}
		return WfUtils.wfBusPrint(form);
	}
	
	/**
	 * 保存新增或者修改的Form对象
	 */
	@Override
	public String save() throws Exception {
		oaShouwenService.save(form);
		return RELOAD;
	}

	/**
	 * 删除一条记录
	 */
	@Override
	public String delete() throws Exception {
		oaShouwenService.delete(id);
		return RELOAD;
	}
	
	/**
	 * 删除多条记录
	 */
	@Override
	public String deleteMore() throws Exception{
		oaShouwenService.delete(checkedIds);
		return RELOAD;
	}
	
	/**
	 * 准备页面模型
	 */
	@Override
	protected void prepareModel() throws Exception {
		LoginUserForm userForm = (LoginUserForm) super.getSession().getAttribute(WebContextUtils.SES_LOGIN_USER);
		
		if(id != null){
			form = oaShouwenService.get(id);
		}
		else
			form = new OaShouwenForm();
		
		if (userForm != null) {
            form.setCurrUserName(userForm.getUser().getUserName());
        }
	}
	

	public OaShouwenForm getModel() {
		return form;
	}
	
	// 属性getter和setter方法
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long[] getCheckedIds() {
		return checkedIds;
	}

	public void setCheckedIds(Long[] checkedIds) {
		this.checkedIds = checkedIds;
	}
	
	public Page<OaShouwenForm> getPage() {
		return page;
	}


	public void setPage(Page<OaShouwenForm> page) {
		this.page = page;
	}
}
